//
//  ZWInteractionBController.m
//  iOSH5Interaction
//
//  Created by zaiwei on 2017/11/5.
//  Copyright © 2017年 home. All rights reserved.
//

#import "ZWInteractionBController.h"
#import <JavaScriptCore/JavaScriptCore.h>
#import "ServerJS.h"

@interface ZWInteractionBController ()<UIWebViewDelegate>
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) ServerJS *js;

@end

@implementation ZWInteractionBController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"交互方式B";
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    
    _webView = [[UIWebView alloc] init];
    _webView.frame = CGRectMake(0, 0, 375, 375);
    _webView.delegate = self;
    [self.view addSubview:_webView];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button1 setTitle:@"修改html的背景色" forState:UIControlStateNormal];
    button1.frame = CGRectMake(100, CGRectGetMaxY(_webView.frame)+5, 200, 50);
    button1.backgroundColor = [UIColor yellowColor];
    [button1 addTarget:self action:@selector(button1Click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button2 setTitle:@"html弹框" forState:UIControlStateNormal];
    button2.frame = CGRectMake(100, CGRectGetMaxY(button1.frame)+5, 200, 50);
    button2.backgroundColor = [UIColor yellowColor];
    [button2 addTarget:self action:@selector(button2Click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
    
    
    
    NSString* htmlPath = [[NSBundle mainBundle] pathForResource:@"webAppB" ofType:@"html"];
    NSString* appHtml = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
    NSURL *baseURL = [NSURL fileURLWithPath:htmlPath];
    [_webView loadHTMLString:appHtml baseURL:baseURL];

}

#pragma mark - userAction
-(void)button1Click{
    
    [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#ffe'"];//调用JS代码修改背景色
}
-(void)button2Click{
    //H5 要提前定义好方法 function alertTest(str)
    [_webView stringByEvaluatingJavaScriptFromString:@"alertTest('hello world!')"];
}


#pragma mark - <UIWebViewDelegate>

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    //把实现<ServerJSProtocol>协议的ServerJS 对象传给H5， H5接下来会调用定义的协议的方法
    JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    _js = [[ServerJS alloc] init];
    context[@"transferEntity"] = _js;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
